<template>
  <view class="address-list">
    <!-- 头部 -->
    <view class="header">
      <text class="title">我的地址</text>
    </view>
    
    <!-- 地址列表 -->
    <scroll-view class="list" scroll-y>
      <view 
        v-for="(item, index) in addressList" 
        :key="item.id" 
        class="address-item"
        @click="selectAddress(item)"
      >
        <view class="info">
          <text class="name">{{item.name}}</text>
          <text class="phone">{{item.phone}}</text>
          <view class="default-tag" v-if="item.isDefault">默认</view>
        </view>
        <view class="address">{{item.province}}{{item.city}}{{item.district}}{{item.detail}}</view>
        <view class="actions">
          <view class="action-item" @click.stop="editAddress(item)">
            <uni-icons type="compose" size="20" color="#666"></uni-icons>
            <text>编辑</text>
          </view>
          <view class="action-item" @click.stop="deleteAddress(item.id, index)">
            <uni-icons type="trash" size="20" color="#666"></uni-icons>
            <text>删除</text>
          </view>
        </view>
      </view>
      
      <view v-if="addressList.length === 0" class="empty">
        <uni-icons type="location" size="60" color="#ccc"></uni-icons>
        <text class="empty-text">暂无收货地址</text>
      </view>
    </scroll-view>
    
    <!-- 添加按钮 -->
    <view class="add-btn" @click="addAddress">
      <text>+ 添加新地址</text>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        addressList: [
          {
            id: 1,
            name: '张三',
            phone: '13800138000',
            province: '广东省',
            city: '深圳市',
            district: '南山区',
            detail: '科技园路1号',
            isDefault: true
          },
          {
            id: 2,
            name: '李四',
            phone: '13900139000',
            province: '北京市',
            city: '北京市',
            district: '海淀区',
            detail: '中关村大街1号',
            isDefault: false
          }
        ],
        isSelectMode: false // 是否是选择地址模式
      }
    },
    onLoad(options) {
      if (options.select === 'true') {
        this.isSelectMode = true
      }
      // 实际项目中这里应该请求接口获取地址列表
      // this.getAddressList()
    },
    methods: {
      // 获取地址列表
      getAddressList() {
        uni.request({
          url: '/api/address/list',
          success: (res) => {
            this.addressList = res.data
          }
        })
      },
      
      // 添加地址
      addAddress() {
        uni.navigateTo({
          url: '/pages/index/adddizhi?type=add'
        })
      },
      
      // 编辑地址
      editAddress(item) {
        uni.navigateTo({
          url: `/pages/index/adddizhi?type=edit&id=${item.id}`
        })
      },
      
      // 删除地址
      deleteAddress(id, index) {
        uni.showModal({
          title: '提示',
          content: '确定要删除该地址吗？',
          success: (res) => {
            if (res.confirm) {
              // 实际项目中这里应该调用删除接口
              this.addressList.splice(index, 1)
              uni.showToast({
                title: '删除成功',
                icon: 'success'
              })
            }
          }
        })
      },
      
      // 选择地址（用于订单确认页选择地址）
      selectAddress(item) {
        if (this.isSelectMode) {
          // 将选择的地址返回给上一个页面
          const pages = getCurrentPages()
          const prevPage = pages[pages.length - 2]
          prevPage.$vm.selectedAddress = item
          uni.navigateBack()
        }
      }
    }
  }
</script>

<style scoped>
  .address-list {
    height: 100vh;
    background-color: #f5f5f5;
    display: flex;
    flex-direction: column;
  }
  
  .header {
    height: 80rpx;
    line-height: 80rpx;
    text-align: center;
    font-size: 32rpx;
    font-weight: bold;
    background-color: #fff;
    border-bottom: 1rpx solid #eee;
  }
  
  .list {
    flex: 1;
    padding: 20rpx;
  }
  
  .address-item {
    background-color: #fff;
    border-radius: 10rpx;
    padding: 20rpx;
    margin-bottom: 20rpx;
  }
  
  .info {
    display: flex;
    align-items: center;
    margin-bottom: 10rpx;
  }
  
  .name {
    font-size: 28rpx;
    font-weight: bold;
    margin-right: 20rpx;
  }
  
  .phone {
    font-size: 28rpx;
    color: #666;
  }
  
  .default-tag {
    background-color: #1296db;
    color: #fff;
    font-size: 20rpx;
    padding: 2rpx 8rpx;
    border-radius: 4rpx;
    margin-left: 20rpx;
  }
  
  .address {
    font-size: 26rpx;
    color: #666;
    line-height: 1.6;
  }
  
  .actions {
    display: flex;
    justify-content: flex-end;
    margin-top: 20rpx;
    padding-top: 20rpx;
    border-top: 1rpx solid #eee;
  }
  
  .action-item {
    display: flex;
    align-items: center;
    margin-left: 30rpx;
    font-size: 26rpx;
    color: #666;
  }
  
  .add-btn {
    height: 90rpx;
    line-height: 90rpx;
    text-align: center;
    background-color: #1296db;
    color: #fff;
    font-size: 30rpx;
    margin: 20rpx;
    border-radius: 45rpx;
  }
  
  .empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 400rpx;
    color: #999;
  }
  
  .empty-text {
    margin-top: 20rpx;
    font-size: 28rpx;
  }
</style>